package com.superzqb.asus.superzqbFragment;


import android.content.Context;
import android.content.Intent;
import android.content.SharedPreferences;
import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.view.Display;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.LinearLayout;

import com.bigkoo.convenientbanner.ConvenientBanner;
import com.bigkoo.convenientbanner.holder.CBViewHolderCreator;
import com.bigkoo.convenientbanner.holder.Holder;
import com.squareup.picasso.Picasso;
import com.superzqb.asus.superzqb.R;
import com.superzqb.asus.superzqbFragment.superzqbJobShop.Enterprise.ConcurrentStudent;
import com.superzqb.asus.superzqbFragment.superzqbJobShop.Home.CampusHome;
import com.superzqb.asus.superzqbFragment.superzqbJobShop.Shop.CampusShop;
import com.superzqb.asus.superzqbFragment.superzqbSave.AddAim;
import com.superzqb.asus.superzqbUtil.GeneralFun;

import java.util.Arrays;
import java.util.List;

import static com.superzqb.asus.Api.ApiParam.imgUrl;


/**
 * Created by asus on 2017/1/17.
 */

public class PjobFragment extends Fragment {
    private LinearLayout job, company, home, save;
    private SharedPreferences sp;
    private ConvenientBanner convenient;//顶部广告栏控件
    private List<String> networkImages;
    private LinearLayout li;
    private ViewGroup.LayoutParams i;

    private String[]  images = {imgUrl+"campus1.png",imgUrl+"campus2.jpg",imgUrl+"campus3.jpg"};
    /*小圆点颜色  ic_page_indicator  #B4CDCD  ic_page_indicator_focused  #9F79EE*/
    public PjobFragment() {
        // Required empty public constructor
    }

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
                             Bundle savedInstanceState) {
        // Inflate the layout for this fragment
        View view = inflater.inflate(R.layout.pjob, container, false);
        initView(view);
        networkImages = Arrays.asList(images);
        convenient.startTurning(3000);
        convenient.setPages(new CBViewHolderCreator<NetworkImageHolderView>() {
            @Override
            public NetworkImageHolderView createHolder() {
                return new NetworkImageHolderView();
            }
        }, networkImages)
                .setPageIndicator(new int[]{R.mipmap.ic_page_indicator, R.mipmap.ic_page_indicator_focused})
                .setPageIndicatorAlign(ConvenientBanner.PageIndicatorAlign.CENTER_HORIZONTAL);
        return view;
    }


    private void initView(View view) {
        job = (LinearLayout) view.findViewById(R.id.jobButton);
        company = (LinearLayout) view.findViewById(R.id.companyButton);
        home = (LinearLayout) view.findViewById(R.id.homeButton);
        save = (LinearLayout) view.findViewById(R.id.saveButton);


        convenient = (ConvenientBanner) view.findViewById(R.id.convenientBanner);
        li = (LinearLayout) view.findViewById(R.id.play);
        /*获取屏幕的宽高*/
        Display d = getActivity().getWindowManager().getDefaultDisplay();
        /*获取布局参数*/
        i = li.getLayoutParams();
        i.height = d.getHeight() / 3;
        i.width = d.getWidth();
        sp = getActivity().getSharedPreferences("uinfo", getActivity().MODE_PRIVATE);
        job.setOnClickListener(listener);
        home.setOnClickListener(listener);
        company.setOnClickListener(listener);
        save.setOnClickListener(listener);
    }

    private View.OnClickListener listener = new View.OnClickListener() {
        @Override
        public void onClick(View v) {
           /* 判断是否登录*/
            if (sp.getString("uid", null) == null) {
                new GeneralFun().login(getActivity());
            } else {
                switch (v.getId()) {
                    case R.id.jobButton:

                        Intent intent = new Intent();
                        intent.setClass(getActivity(), CampusShop.class);
                        startActivity(intent);
                        break;
                    case R.id.homeButton:
                        Intent intent1 = new Intent();
                        intent1.setClass(getActivity(), CampusHome.class);
                        startActivity(intent1);
                        break;
                    case R.id.companyButton:
                        Intent intent2 = new Intent();
                        intent2.setClass(getActivity(),ConcurrentStudent.class);
                        startActivity(intent2);
                        break;
                    case R.id.saveButton:
                        Intent intent3 = new Intent();
                        intent3.setClass(getActivity(),AddAim.class);
                        startActivity(intent3);
                        break;
                }
            }
        }
    };

    private class NetworkImageHolderView implements Holder<String> {
        private ImageView imageView;

        @Override
        public View createView(Context context) {
            imageView = new ImageView(context);
            imageView.setScaleType(ImageView.ScaleType.CENTER_CROP);
            return imageView;
        }

        @Override
        public void UpdateUI(Context context, int position, String data) {
          /*  Picasso.with(getActivity()).load(data).transform(new BitmapTransformation(i)).into(imageView);*/
            Picasso.with(getActivity()).load(data).resize(i.width, i.height).into(imageView);
        }
    }

}
